<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 居民信息管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-select v-model="query.isPutaway" placeholder="是否上架" class="handle-select mr10" clearable>
                    <el-option key="1" label="已上架" value="1"></el-option>
                    <el-option key="2" label="已下架" value="0"></el-option>
                </el-select>
                <el-select v-model="query.type" placeholder="分类" class="handle-select mr10" clearable filterable>
                    <div v-for="(item, index) in typeList" :key="index">
                        <el-option key="1" :label="item.name" :value="item.name"></el-option>
                    </div>
                </el-select>
                <el-input v-model="query.id" placeholder="书籍编号" class="handle-input mr10" style="width: 100px"></el-input>
                <el-input v-model="query.title" placeholder="书籍标题" class="handle-input mr10" style="width: 200px"></el-input>
                <el-input v-model="query.publisher" placeholder="出版社" class="handle-input mr10" style="width: 200px"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="bookList"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="title" label="姓名"></el-table-column>
                <el-table-column prop="type" label="性别"></el-table-column>
                <el-table-column prop="description" label="身高"></el-table-column>
                <el-table-column label="体重">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="showAuthor(scope.row.authorId)">查看作者信息</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="publisher" label="年龄"></el-table-column>
                <el-table-column prop="publishDate" label="邮箱"></el-table-column>

                <!-- <el-table-column label="封面(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.coverUrl" :preview-src-list="[scope.row.coverUrl]"></el-image>
                    </template>
                </el-table-column> -->
                <el-table-column label="今日状态" align="center">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.isPutaway === 1 ? 'success' : 'danger'">{{
                            scope.row.isPutaway === 1 ? '正常' : '异常'
                        }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                            type="text"
                            :icon="scope.row.isPutaway == 1 ? 'el-icon-caret-bottom' : 'el-icon-caret-top'"
                            :class="scope.row.isPutaway == 1 ? 'red' : ''"
                            @click="putAwayOrNotBook(scope.$index, scope.row)"
                        >
                            {{ scope.row.isPutaway == 1 ? '下架' : '上架' }}
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑书籍信息弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="bookForm" :model="book" :rules="bookrule" label-width="70px">
                <el-form-item label="书籍编号">
                    <el-input v-model="book.id" disabled></el-input>
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input v-model="book.title"></el-input>
                </el-form-item>
                <el-form-item label="分类" prop="type">
                    <el-select v-model="book.type" placeholder="分类" class="handle-select mr10" clearable>
                        <div v-for="(item, index) in typeList" :key="index">
                            <el-option key="1" :label="item.name" :value="item.name"></el-option>
                        </div>
                    </el-select>
                </el-form-item>
                <el-form-item label="作者ID" prop="authorId">
                    <el-input v-model="book.authorId" style="width: 100px"></el-input>
                </el-form-item>
                <el-form-item label="出版社" prop="publisher">
                    <el-input v-model="book.publisher"></el-input>
                </el-form-item>
                <el-form-item label="简介" prop="description">
                    <el-input v-model="book.description" type="textarea" :rows="3"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit('bookForm')">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 作者信息弹出框 -->
        <el-dialog title="作者信息" :visible.sync="showAuthorVisible" width="30%">
            <el-form ref="form" label-width="70px">
                <el-form-item label="编号">
                    {{ book.author.id }}
                </el-form-item>
                <el-form-item label="姓名">
                    {{ book.author.name }}
                </el-form-item>
                <el-form-item label="性别">
                    {{ book.author.sex == 1 ? '男' : '女' }}
                </el-form-item>
                <el-form-item label="出生日期">
                    {{ book.author.birthday }}
                </el-form-item>
                <el-form-item label="简介">
                    {{ book.author.description }}
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="showAuthorVisible = false">关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        var checkAuthorId = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('作者编号不能为空'));
            }
            if (!Number.isInteger(+value)) {
                callback(new Error('请输入数字值'));
            } else {
                return callback();
            }
        };
        return {
            query: {
                isPutaway: '',
                pageIndex: 1,
                id: '',
                title: '',
                type: '',
                authorId: '',
                publisher: ''
            },
            typeList: [],
            bookList: [],
            book: {
                id: '',
                title: '',
                type: '',
                description: '',
                authorId: '',
                publisher: '',
                author: []
            },
            bookrule: {
                title: [{ required: true, message: '请输入书籍名称', trigger: 'blur' }],
                authorId: [{ validator: checkAuthorId, trigger: 'blur' }],
                type: [{ required: true, message: '请选择书籍类型', trigger: 'change' }],
                publisher: [{ required: true, message: '请输入出版社名称', trigger: 'blur' }],
                publishDate: [{ required: true, message: '请选择出版日期', trigger: 'change' }],
                description: []
            },
            editVisible: false,
            showAuthorVisible: false,
            showCoverVisible: false,
            multipleSelection: [],
            delList: [],
            pageTotal: 0,
            pageSize: 10,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.handleSearch();
        this.$http.get('/type/list').then((res) => {
            this.typeList = res.data.data;
        });
    },
    methods: {
        // 触发搜索按钮
        handleSearch() {
            this.$http.post('/book/query', this.query).then((res) => {
                this.bookList = res.data.data.list;
                this.pageTotal = res.data.data.total;
                // console.log(res.data.data);
            });
        },
        // 下架或者上架书籍
        putAwayOrNotBook(index, row) {
            this.$http.post('/book/putawayOrNot/' + row.id).then((res) => {
                var book = res.data.data;
                console.log(res.data);
                if (res.data.status == 1) {
                    if (book.isPutaway == 1) {
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.warning(res.data.message);
                    }
                    this.$set(this.bookList, index, book);
                } else {
                    this.$message.error(res.data.message);
                    console.log(res.data)
                    if (res.data.status == 401) {
                        this.$router.push({ path: '/login' });
                    }
                }
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            console.log(row);
            this.book = JSON.parse(JSON.stringify(row));
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit(formName) {
            this.$refs[formName].validate((valid) => {
                this.$http.post('/book/update', this.book).then((res) => {
                    if (res.data.status == 1) {
                        this.$set(this.bookList, this.idx, this.book);
                        this.$message.success(`修改 ${this.book.id} 号书籍成功`);
                    } else {
                        this.$message.error(res.data.message);
                    }
                    this.editVisible = false;
                });
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.query.pageIndex = val;
            this.handleSearch();
        },
        showAuthor(authorId) {
            console.log('打开作者信息');
            this.$http.get('/author/detail/' + authorId).then((res) => {
                this.book.author = res.data.data;
                this.showAuthorVisible = true;
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
